<template>
  <div id="app">
    <router-view v-if="isLoginPage" />
    
    <el-container v-else class="main-container">
      <!-- 头部 -->
      <el-header class="main-header" height="60px">
        <div class="header-left">
          <h2 class="system-title">PCB ERP管理系统</h2>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleUserCommand">
            <span class="user-info">
              <el-icon><User /></el-icon>
              {{ currentUser.employeeName || '用户' }}
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-container class="content-container">
        <!-- 侧边栏 -->
        <el-aside width="220px" class="main-sidebar">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical"
            router
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#ffffff"
          >
            <el-menu-item index="/supplier">
              <el-icon><Shop /></el-icon>
              <span>供应商管理</span>
            </el-menu-item>
            <el-menu-item index="/customer">
              <el-icon><UserFilled /></el-icon>
              <span>客户管理</span>
            </el-menu-item>
            <el-menu-item index="/employee">
              <el-icon><Avatar /></el-icon>
              <span>员工管理</span>
            </el-menu-item>
            <el-sub-menu index="order">
              <template #title>
                <el-icon><Document /></el-icon>
                <span>订单与报价管理</span>
              </template>
              <el-menu-item index="/quotation">报价单</el-menu-item>
              <el-menu-item index="/quotation-record">报价记录</el-menu-item>
              <el-menu-item index="/contract-audit">合同审核</el-menu-item>
              <el-menu-item index="/outsourcing">外发加工</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="warehouse">
              <template #title>
                <el-icon><Box /></el-icon>
                <span>仓库管理</span>
              </template>
              <el-menu-item index="/inbound">入库</el-menu-item>
              <el-menu-item index="/inbound-record">入库记录</el-menu-item>
              <el-menu-item index="/outbound">出库</el-menu-item>
              <el-menu-item index="/outbound-record">出库记录</el-menu-item>
              <el-menu-item index="/inventory">库存</el-menu-item>
            </el-sub-menu>
            <el-menu-item index="/label-maker">
              <el-icon><Printer /></el-icon>
              <span>标签制作</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        
        <!-- 主内容区域 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
    
    <!-- 个人中心对话框 -->
    <el-dialog v-model="showProfileDialog" title="个人中心" width="500px">
      <el-form :model="currentUser" label-width="80px">
        <el-form-item label="员工代码">
          <el-input v-model="currentUser.employeeCode" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="currentUser.employeeName" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-input :value="roleDisplay" disabled></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showProfileDialog = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, ArrowDown, Shop, UserFilled, Avatar, Document, Box, Printer } from '@element-plus/icons-vue'

export default {
  name: 'App',
  components: {
    User, ArrowDown, Shop, UserFilled, Avatar, Document, Box, Printer
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const showProfileDialog = ref(false)
    
    // 判断是否为登录页
    const isLoginPage = computed(() => route.path === '/login')
    
    // 当前激活的菜单项
    const activeMenu = computed(() => route.path)
    
    // 当前用户信息
    const currentUser = reactive({
      id: '',
      employeeCode: '',
      employeeName: '',
      role: ''
    })
    
    // 角色显示
    const roleDisplay = computed(() => {
      switch (currentUser.role) {
        case 'admin':
          return '系统管理员'
        case 'manager':
          return '业务经理'
        default:
          return currentUser.role
      }
    })
    
    // 方法
    const handleUserCommand = (command) => {
      switch (command) {
        case 'profile':
          showProfileDialog.value = true
          break
        case 'logout':
          handleLogout()
          break
      }
    }
    
    const handleLogout = () => {
      ElMessageBox.confirm(
        '确定要退出登录吗？',
        '确认退出',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        // 清除本地存储的用户信息
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        
        ElMessage.success('已退出登录')
        
        // 跳转到登录页面
        router.push('/login')
      }).catch(() => {
        // 用户取消
      })
    }
    
    // 加载用户信息
    const loadUserInfo = () => {
      const userInfoStr = localStorage.getItem('userInfo')
      if (userInfoStr) {
        try {
          const userInfo = JSON.parse(userInfoStr)
          currentUser.id = userInfo.id
          currentUser.employeeCode = userInfo.employeeCode
          currentUser.employeeName = userInfo.employeeName
          currentUser.role = userInfo.role
        } catch (error) {
          console.error('解析用户信息失败:', error)
        }
      }
    }
    
    // 生命周期
    onMounted(() => {
      // 加载用户信息
      loadUserInfo()
    })
    
    return {
      isLoginPage,
      activeMenu,
      currentUser,
      roleDisplay,
      showProfileDialog,
      handleUserCommand,
      handleLogout
    }
  }
}
</script>

<style>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background-color: #f5f6fa;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

#app {
  height: 100vh;
  width: 100vw;
}

.main-container {
  height: 100%;
  width: 100%;
}

.content-container {
  height: calc(100% - 60px);
  width: 100%;
}

/* 头部样式 */
.main-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  height: 60px !important;
}

.header-left .system-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.header-right .user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
  color: white;
}

.header-right .user-info:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

/* 侧边栏样式 */
.main-sidebar {
  background-color: #304156;
  border-right: 1px solid #dcdfe6;
  overflow-y: auto;
}

.el-menu-vertical {
  height: 100%;
  border-right: none;
}

.el-menu-vertical .el-sub-menu__title, .el-menu-vertical .el-menu-item {
  height: 50px;
  line-height: 50px;
}

/* 主内容区域样式 */
.main-content {
  background-color: #f5f6fa;
  padding: 20px;
  overflow-y: auto;
}
</style>
